package com.landeb.shop.huo.adapter;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.landeb.shop.R;
import com.landeb.shop.huo.act.OrderDetailActivity;
import com.landeb.shop.huo.act.PayActivity;
import com.landeb.shop.huo.bean.BeanFragOrderRequestPayNow;
import com.landeb.shop.huo.bean.BeanOrderFrag;
import com.landeb.shop.huo.bean.BeanStatusInfo;
import com.landeb.shop.huo.frag.OrderFragment;
import com.landeb.shop.huo.httpRequest.FragOrderRequest;
import com.landeb.shop.huo.view.HorizontalListView;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by blade on 2016/11/24.
 */

public class AllOrderFragLVAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<BeanOrderFrag.DataBean.ListBean> mList;
    private FragOrderRequest mRequest;
    private OrderFragment mFrag;

    public AllOrderFragLVAdapter(Context context, List<BeanOrderFrag.DataBean.ListBean> list) {
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(context);
    }

    public AllOrderFragLVAdapter(Context context, List<BeanOrderFrag.DataBean.ListBean> list, FragOrderRequest request, OrderFragment frag) {
        mContext = context;
        mList = list;
        mRequest = request;
        mFrag = frag;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        payHolder holder = null;
        if (convertView == null) {
            holder = new payHolder();

            convertView = mInflater.inflate(R.layout.item_pay_all_order_frag_lv, parent, false);

            holder.mShop = (TextView) convertView.findViewById(R.id.tv_shopName_item_pay_all_order_frag_lv);
            holder.mStatus = (TextView) convertView.findViewById(R.id.tv_status_item_pay_all_order_frag_lv);

            holder.mHorizontalListView = (HorizontalListView) convertView.findViewById(R.id.horizontalLV_item_pay_all_order_frag_lv);


            holder.mGoodsCount = (TextView) convertView.findViewById(R.id.tv_goodsCount_item_pay_all_order_frag_lv);
            holder.mGoodsPrice = (TextView) convertView.findViewById(R.id.tv_goodsPrice_item_pay_all_order_frag_lv);
            holder.mGoodsExpress = (TextView) convertView.findViewById(R.id.tv_goodsExpenses_item_pay_all_order_frag_lv);

            holder.mBtnRight = (Button) convertView.findViewById(R.id.btn_right_item_pay_all_order_frag_lv);
            holder.mBtnLeft = (Button) convertView.findViewById(R.id.btn_left_item_pay_all_order_frag_lv);


            convertView.setTag(holder);
        } else {
            holder = (payHolder) convertView.getTag();
        }

        final BeanOrderFrag.DataBean.ListBean order = mList.get(position);

        holder.mShop.setText(order.getStorename());

        if (order.getGoods() != null) {
            HorizonListviewAdapter mAdapter = new HorizonListviewAdapter(mContext, order.getGoods());
            holder.mHorizontalListView.setAdapter(mAdapter);
        }

        holder.mHorizontalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO: 2016/11/24 订单详情
                String ID = order.getId();
                OrderDetailActivity.toAct(mContext, ID);
            }
        });

        holder.mGoodsCount.setText("共计" + order.getCount_goods() + "件商品");
        holder.mGoodsPrice.setText("合计：¥" + order.getMoney());
        holder.mGoodsExpress.setText("运费：¥" + order.getExpress_money());

        String status = order.getStatus();

        if (status.equals("unpay")) {

            holder.mStatus.setText("等待付款");

            holder.mBtnLeft.setVisibility(View.VISIBLE);
            holder.mBtnLeft.setText("立即付款");
            holder.mBtnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2016/11/24 立即付款
                    String id = order.getId();

                    final ProgressDialog dialog = new ProgressDialog(mContext);
                    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    dialog.setMessage("正在提交，请稍后");
                    dialog.setCancelable(false);
                    dialog.show();

                    mRequest.payNow(id)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Subscriber<BeanFragOrderRequestPayNow>() {
                                @Override
                                public void onCompleted() {
                                    dialog.dismiss();
                                }

                                @Override
                                public void onError(Throwable e) {
                                    dialog.dismiss();
                                }

                                @Override
                                public void onNext(BeanFragOrderRequestPayNow beanFragOrderRequestPayNow) {


                                    if (beanFragOrderRequestPayNow.getStatus() == 1) {

                                        PayActivity.toAct(mContext, beanFragOrderRequestPayNow.getData().getId());


                                    } else {


                                        Toast.makeText(mContext, beanFragOrderRequestPayNow.getInfo(), Toast.LENGTH_SHORT).show();

                                    }



                                }
                            });

//                    Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();

                }
            });

            holder.mBtnRight.setText("取消订单");
            holder.mBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2016/11/24 取消订单
                    final String id = order.getId();
//                    Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();

                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setTitle("系统提示").setMessage("确定取消此订单").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {


                            final ProgressDialog dialogProgress = new ProgressDialog(mContext);
                            dialogProgress.setMessage("请稍后");
                            dialogProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                            dialogProgress.show();

                            mRequest.cancelOrder(id)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Subscriber<BeanStatusInfo>() {
                                        @Override
                                        public void onCompleted() {
                                            Log.e("cancelOrder", "onCompleted: ");
                                            dialogProgress.dismiss();
                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                            Log.e("cancelOrder", "onError: " + e.toString());
                                            dialogProgress.dismiss();

                                        }

                                        @Override
                                        public void onNext(BeanStatusInfo beanStatusInfo) {

                                            if (beanStatusInfo.getStatus() == 1) {

                                                Toast.makeText(mContext, beanStatusInfo.getInfo(), Toast.LENGTH_SHORT).show();
                                                mFrag.refresh();
                                            } else {

                                                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

                                                builder.setTitle("系统提示")
                                                        .setMessage(beanStatusInfo.getInfo())
                                                        .setCancelable(false)
                                                        .setPositiveButton("确定", null).show();

                                            }


                                        }
                                    });


                        }
                    }).setNegativeButton("取消", null).create().show();


                }
            });

        } else if (status.equals("pay")) {

            holder.mStatus.setText("已付款");

            holder.mBtnLeft.setVisibility(View.GONE);

            holder.mBtnRight.setText("订单详情");
            holder.mBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2016/11/24 订单详情
                    String id = order.getId();
//                    Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();
                    OrderDetailActivity.toAct(mContext, id);

                }
            });

        } else if (status.equals("send")) {

            holder.mStatus.setText("已发货");

            holder.mBtnLeft.setVisibility(View.VISIBLE);
            holder.mBtnLeft.setText("查看物流");
            holder.mBtnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2016/11/24 查看物流
                    String id = order.getId();
                    Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();


                }
            });

            holder.mBtnRight.setText("确认收货");
            holder.mBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2016/11/24 确认收货
                    String id = order.getId();
//                    Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();

                    mRequest.ConfirmReceipt(id)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Subscriber<BeanStatusInfo>() {
                                @Override
                                public void onCompleted() {
                                    Log.e("ConfirmReceipt", "onCompleted: ");
                                }

                                @Override
                                public void onError(Throwable e) {
                                    Log.e("ConfirmReceipt", "onError: " + e.toString());

                                }

                                @Override
                                public void onNext(BeanStatusInfo beanStatusInfo) {

                                    if (beanStatusInfo.getStatus() == 1) {

                                        Toast.makeText(mContext, beanStatusInfo.getInfo(), Toast.LENGTH_SHORT).show();

                                        mFrag.refresh();

                                    } else {

                                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

                                        builder.setTitle("系统提示")
                                                .setMessage(beanStatusInfo.getInfo())
                                                .setCancelable(false)
                                                .setPositiveButton("确定", null).show();

                                    }


                                }
                            });


                }
            });


        } else if (status.equals("cancel")) {

            holder.mStatus.setText("已取消");

            holder.mBtnLeft.setVisibility(View.GONE);

            holder.mBtnRight.setText("订单详情");
            holder.mBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2016/11/24 订单详情
                    String id = order.getId();
//                    Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();
                    OrderDetailActivity.toAct(mContext, id);


                }
            });

        } else if (status.equals("finish")) {

            holder.mStatus.setText("已收货");

            holder.mBtnLeft.setVisibility(View.GONE);

            holder.mBtnRight.setText("确认晒单");
            holder.mBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2016/11/24 确认晒单
                    String id = order.getId();
                    Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();
                }
            });

        }


        return convertView;
    }

    private class payHolder {
        private TextView mShop, mStatus; //文字
        private HorizontalListView mHorizontalListView;
        private Button mBtnRight, mBtnLeft;
        private TextView mGoodsCount, mGoodsPrice, mGoodsExpress;
    }

}
